package com.platform.upms.service.impl;

import com.platform.common.base.impl.BaseServiceImpl;
import com.platform.common.page.Page;
import com.platform.common.redis.RedisUtil;
import com.platform.common.utils.StringUtils;
import com.platform.upms.dao.PositionMenuDao;
import com.platform.upms.dao.UpmsMenuDao;
import com.platform.upms.dao.UpmsOrgMenuDao;
import com.platform.upms.model.UpmsMenu;
import com.platform.upms.model.UpmsOrg;
import com.platform.upms.model.ZNode;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UpmsMenuServiceImpl extends BaseServiceImpl<UpmsMenu> implements UpmsMenuService {

    @Autowired
    private UpmsMenuDao upmsMenuDao;

    @Autowired
    private UpmsOrgMenuDao upmsOrgMenuDao;

    @Autowired
    private PositionMenuDao positionMenuDao;

    @Override
    public List<UpmsMenu> list(UpmsMenu upmsMenu) {
      List<UpmsMenu> list = upmsMenuDao.selectList(upmsMenu);
        return list;
    }

    @Override
    public UpmsMenu getMenu() {
        UpmsMenu upmsMenu =upmsMenuDao.getMenu();
        return upmsMenu;
    }

    @Override
    public List<ZNode> findZnodeList(UpmsOrg upmsOrg) {
        return upmsMenuDao.findZnodeList(upmsOrg);
    }

    @Override
    public Page<UpmsMenu> findByPageRedis(Page<UpmsMenu> page, UpmsMenu upmsMenu) {
//        Jedis jedis = RedisUtil.getJedis();
//        Set<String> zrange = jedis.zrange("menu"+page.getPageNum(),0,10);
//        if(zrange.size()==0){
            page.setResults(upmsMenuDao.findByPage(page,upmsMenu));
//            for (UpmsMenu upmsMenu1 :page.getResults()){
//                try{
//                    jedis.zadd("menu"+page.getPageNum(),1, StringUtils.objectToJson(upmsMenu1));
//                    } catch (Exception e){
//
//                }
//            }
//            jedis.set("menulist"+page.getPageNum(),""+page.getTotalPage());
//            jedis.set("menulist"+page.getPageNum()+"s",""+page.getTotalRecord());
        return  page;
//        }
//        else{
//            List<UpmsMenu> upmsMenus = new ArrayList<>();
//            Page<UpmsMenu> pageList= new Page<>();
//
//            for(String s:zrange){
//                UpmsMenu upmsMenu1 = StringUtils.json2Object(s,UpmsMenu.class);
//                upmsMenus.add(upmsMenu1);
//            }
//            String num = jedis.get("menulist"+page.getPageNum());
//            String rec = jedis.get("menulist"+page.getPageNum()+"s");
//            pageList.setTotalPage(Integer.valueOf(num).intValue());
//            pageList.setTotalRecord(Integer.valueOf(rec).intValue());
//            pageList.setPageNum(page.getPageNum());
//            pageList.setResults(upmsMenus);
//          return  pageList;
//        }
    }

    @Override
    public List<UpmsMenu> listMenuByUserId(Integer id) {
        List<UpmsMenu> list = getMainMenu(upmsMenuDao.listMenuByUserId(id));
        return list;
    }

    @Override
    public Tip deleteMenuAndPower(Integer[] ids) {
        Tip tip = new Tip("删除成功");
        try {
            //根据菜单id删除组织权限
            upmsOrgMenuDao.deleteByMenuIds(ids);
            //根据菜单id删除职务权限
            positionMenuDao.deleteByMenuIds(ids);
            //删除菜单
            upmsMenuDao.deleteDataById(ids);
        } catch (Exception e) {
            tip = new Tip(1, "系统异常，删除失败");
        }

        return tip;
    }

    private List<UpmsMenu> getMainMenu(List<UpmsMenu> list) {
        List<UpmsMenu> array = new ArrayList<>();
        list.forEach(l -> {
            if(l.getpId() == 1) {
                array.add(l);
            }
        });
        array.forEach(a -> a.setLevelList(getMenuList(a.getId(),list)));
        return array;
    }

    private List<UpmsMenu> getMenuList(Integer id, List<UpmsMenu> list) {
        List<UpmsMenu> array = new ArrayList<>();
        list.forEach(l -> {
            if(l.getpId() == id) {
                array.add(l);
            }
        });
        if(array.size() == 0) {
            return null;
        }
        array.forEach(a -> a.setLevelList(getMenuList(a.getId(),list)));
        return array;
    }

}
